﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SilverlightMediaFramework.Plugins.DualMedia;
using Microsoft.SilverlightMediaFramework.Plugins.Webcam;

namespace Test.DualWebcam
{
    public partial class MainPage : UserControl
    {
        private DualMediaPlugin S3DPlugin;

        public MainPage()
        {
            InitializeComponent();
            smf.MediaPluginRegistered += new EventHandler<Microsoft.SilverlightMediaFramework.Core.CustomEventArgs<Microsoft.SilverlightMediaFramework.Plugins.IMediaPlugin>>(smf_MediaPluginRegistered);
        }

        void smf_MediaPluginRegistered(object sender, Microsoft.SilverlightMediaFramework.Core.CustomEventArgs<Microsoft.SilverlightMediaFramework.Plugins.IMediaPlugin> e)
        {
            S3DPlugin = e.Value as DualMediaPlugin;
            S3DPlugin.LeftSource = new WebcamMediaPlugin()
            {
                CaptureSource = new CaptureSource()
                {
                    VideoCaptureDevice = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices().First()
                }
            };

            S3DPlugin.RightSource = new WebcamMediaPlugin()
            {
                CaptureSource = ((WebcamMediaPlugin)S3DPlugin.LeftSource).CaptureSource
            };

            // test with 2 different cameras
            //S3DPlugin.RightSource = new WebcamMediaPlugin()
            //{
            //    CaptureSource = new CaptureSource()
            //    {
            //        VideoCaptureDevice = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices().Skip(1).First()
            //    }
            //};
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            StupidStartButton.Visibility = System.Windows.Visibility.Collapsed;
            ChooseCameras();
        }

        private void ChooseCameras()
        {
            if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
            {
                if (CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices().Count == 0)
                {
                    ErrorMessage.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    smf.CurrentPlaylistItem = smf.Playlist.FirstOrDefault();
                }
            }
            else
            {
                ErrorMessage.Visibility = System.Windows.Visibility.Visible;
            }
        }
    }
}
